const Minio = require('minio');
require('dotenv').config();

// 初始化 MinIO 客户端
const minioClient = new Minio.Client({
  endPoint: 'localhost',
  port: 9090,
  useSSL: false,
  accessKey: process.env.MINIO_ROOT_USER,
  secretKey: process.env.MINIO_ROOT_PASSWORD
});

const bucketName = "wuxing";

// 上传文件
async function uploadFile(objectName, filePath, metaData) {
  await minioClient.fPutObject(bucketName, objectName, filePath, metaData);
  return getFileUrl(objectName);
}

// 从buffer上传文件
async function uploadFileBuffer(objectName, buffer, metaData) {
  await minioClient.putObject(bucketName, objectName, buffer, metaData);
  return getFileUrl(objectName);
}

// 获取文件URL
function getFileUrl(objectName) {
  return `https://minio.mayflower.chat/minio/${bucketName}/${objectName}`;
}

module.exports = {
  minioClient,
  uploadFile,
  uploadFileBuffer,
  getFileUrl
};